#include "MMOrderSettingForm.h"
#include "ui_MMOrderSettingForm.h"
#include <QMetaEnum>
#include <core/StyleMgr.h>
#include <array>

MMOrderSettingForm::MMOrderSettingForm(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::MMOrderSettingForm)
{
    ui->setupUi(this);
    std::array<QPushButton*, 9> buttons{ui->reset,
                                         ui->to_left, ui->to_right,
                                         ui->to_top, ui->to_bottom,
                                         ui->to_upper, ui->to_lower,
                                         ui->confirm, ui->cancel};
    for (QPushButton* btn : buttons) {
        connect(btn, &QPushButton::clicked, this, [this](bool checked){
            QMetaEnum metaButtonId = QMetaEnum::fromType<MMOrderSettingForm::ButtonId>();
            bool isOk = false;
            const int button_id = metaButtonId.keyToValue(this->sender()->objectName().toStdString().c_str(), &isOk);
            if (isOk)
                emit buttonClicked(static_cast<ButtonId>(button_id), checked);
        });
    }

    QString styleSheet = QString(
"MMOrderSettingForm {"
"  background-color: #%1; "
"  border-radius: 3px;"
"  border: 2px solid rgb(35,170,166);"
"  color: #%2;"
"}"
"MMOrderSettingForm QLabel {"
"  color: #%2;"
"}"
"MMOrderSettingForm QPushButton {"
"  color: #%2;"
"  background-color: #%3; "
"  border-radius: 2px;"
"  outline: none;"
"}"
"MMOrderSettingForm QPushButton:hover {"
"  background-color: #%4;"
"}"
"MMOrderSettingForm QPushButton:pressed {"
"  background-color: #%5; "
"}"
"MMOrderSettingForm QPushButton:!hover#cancel {"
"  background-color: #3e5960; "
"}"
"MMOrderSettingForm .QFrame {"
"  border-radius: 3px;"
"  border: 1px solid #%6;"
"}"
"MMOrderSettingForm QPushButton#to_left {"
"  image: url(./res/arrow-left-s-line.svg);"
"}"
"MMOrderSettingForm QPushButton#to_right {"
"  image: url(./res/arrow-right-s-line.svg);"
"}"
"MMOrderSettingForm QPushButton#to_top {"
"  image: url(./res/arrow-up-double-line.svg);"
"}"
"MMOrderSettingForm QPushButton#to_bottom {"
"  image: url(./res/arrow-down-double-line.svg);"
"}"
"MMOrderSettingForm QPushButton#to_upper {"
"  image: url(./res/arrow-up-s-line.svg);"
"}"
"MMOrderSettingForm QPushButton#to_lower {"
"  image: url(./res/arrow-down-s-line.svg);"
"}"
"MMOrderSettingForm QScrollArea {"
"  border: none;"
"  outline: none;"
"  background: transparent;"
"}"
"MMOrderSettingForm QScrollBar:vertical {"
"  background: transparent;"
"  width: 8px;"
"}"
"MMOrderSettingForm QScrollBar::handle {"
"  border-radius:4px; "
"  background-color: #%6;"
"}"
"MMOrderSettingForm QScrollBar::handle:hover {"
"  background-color: #%4;"
"}"
"MMOrderSettingForm QScrollBar::handle:pressed {"
"  background-color: #%7;"
"}"
"MMOrderSettingForm QScrollBar::add-line:vertical{"
"  background: transparent;"
"}"
"MMOrderSettingForm QScrollBar::sub-line:vertical{"
"  background: transparent;"
"}"
"MMOrderSettingForm QScrollBar::add-page:vertical {"
"  background: transparent;"
"}"
"MMOrderSettingForm QScrollBar::sub-page:vertical {"
"  background: transparent;"
"} "
"MMOrderSettingForm QListWidget  {"
"  border: none;"
"  outline: none;"
"  color: #%8;"
"  background: transparent;"
"}"
"MMOrderSettingForm QListWidget::item  {"
"  width: 60px;"
"  background: #%5;"
"  margin-bottom: 3px;"
"  margin-top: 1px;"
"  margin-right: 2px;"
"  height: 32px;"
"  color: #%2;"
"}"
"MMOrderSettingForm QListWidget::item:hover {"
"  background: #%7;"
"}"
"MMOrderSettingForm QListWidget::item:selected {"
"  background: #3c5257;"
"}"
"MMOrderSettingForm QListWidget::indicator {"
"  width: 18px; height: 18px;"
"  background: transparent;"
"  image: url(./res/checkbox-blank-line.svg);"
"}"
"MMOrderSettingForm QListWidget::indicator:checked {"
"  image: url(./res/checkbox-line.svg);"
"}").arg(StyleMgr::instance().color("ElementBackground").rgb(), 0, 16)
    .arg(StyleMgr::instance().color("ChartH8").rgb(), 0, 16)
    .arg(StyleMgr::instance().color("ButtonPressed").rgb(), 0, 16)
    .arg(StyleMgr::instance().color("Hover").rgb(), 0, 16)
    .arg(StyleMgr::instance().color("ButtonDisable").rgb(), 0, 16)
    .arg(StyleMgr::instance().color("TipsBorder").rgb(), 0, 16)
    .arg(StyleMgr::instance().color("MenuTitleBack").rgb(), 0, 16)
    .arg(StyleMgr::instance().color("BrokerAll").rgb(), 0, 16)        ;
    this->setStyleSheet(styleSheet);
}

MMOrderSettingForm::~MMOrderSettingForm()
{
    delete ui;
}

void MMOrderSettingForm::setContentWidget(QWidget* left, QWidget* right)
{
    ui->scrollAreaLeft->setWidget(left);
    ui->scrollAreaRight->setWidget(right);
}

void MMOrderSettingForm::setStatistic(const QString &txt)
{
    ui->statistic->setText(txt);
}

void MMOrderSettingForm::enableButton(const QString& name, bool enable)
{
    QPushButton* btn = findChild<QPushButton*>(name);
    if (btn) {
        btn->setEnabled(enable);
    }
}



